package cn.com.zhbs.account.service.impl;

import cn.com.zhbs.account.dao.AccountsMapper;
import cn.com.zhbs.account.dao.DepartmentMapper;
import cn.com.zhbs.account.service.AccountService;
import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import cn.com.zhbs.common.util.Encrypt;
import cn.com.zhbs.account.entitys.Account;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {

	@Resource
	private AccountsMapper accountsMapper;
	@Resource
	DepartmentMapper departmentMapper;

	@Override
	public Map<String,Object> addAccount(JSONObject jsonObject) throws Exception{
		List<JSONObject> isExist = accountsMapper.isAccountExist(jsonObject);
		if(isExist.size()==0){
			//添加用户信息
			String password =Encrypt.encrypt(jsonObject.get(Account.account.toString()).toString()+jsonObject.get(Account.password.toString()).toString());
			jsonObject.put(Account.password.toString(),password);
			accountsMapper.insertAccount(jsonObject);
			System.out.println(jsonObject.getString("aid"));
			//添加用户角色
			List<String> roles = JSON.parseArray(jsonObject.get("roles").toString(),String.class);
			for(String role:roles){
				jsonObject.put("role_id",role);
				accountsMapper.insertRole(jsonObject);
			}
			//添加用户部门
			List<String> dids = JSON.parseArray(jsonObject.get("departments").toString(),String.class);
			for(String did:dids){
				jsonObject.put(Account.did.toString(),did);
				accountsMapper.insertDepartment(jsonObject);
			}
			return ResultFactory.success().display();
		}else{
			return ResultFactory.failure("0","账户已存在",null).display();
		}
	}

	@Override
	public Map<String,Object> accountList(JSONObject jsonObject) throws Exception {
		Map<String,Object> resultMap=new HashMap<>();
		Integer currentPage=Integer.parseInt(jsonObject.getString("currentPage").toString());
		Integer pageSize=Integer.parseInt(jsonObject.getString("pageSize").toString());
		Integer start=(currentPage-1)*pageSize;
		jsonObject.put("start",start);
		jsonObject.put("pageSize",pageSize);
		jsonObject.put("dids",departmentMapper.findDIDByAID(jsonObject));
		List<JSONObject> list=accountsMapper.findAccountList(jsonObject);
		for(JSONObject account:list){
			account.put("department",accountsMapper.findDNameByAID(account));
		}
		resultMap.put("total",accountsMapper.findAccountTotal(jsonObject));
		resultMap.put("list",list);
		return resultMap;
	}

	@Override
	public Map<String,Object> editAccount(JSONObject jsonObject) throws Exception {
		//修改用户密码（对，改个密码都要写这么多蛋疼的代码）
		if(accountsMapper.isPhoneExist(jsonObject)==null||
				accountsMapper.isPhoneExist(jsonObject).equals(jsonObject.get(Account.account.toString()).toString())) {
			if(jsonObject.get(Account.password.toString())!=""){
				String password = Encrypt.encrypt(jsonObject.get(Account.account.toString()).toString() +
						jsonObject.get(Account.password.toString()).toString());
				jsonObject.put(Account.password.toString(),password);
			}
			accountsMapper.updateAccount(jsonObject);

			//首先删除原有的机构和角色
			accountsMapper.deleteDepartByAID(jsonObject);
			accountsMapper.deleteRolesByAID(jsonObject);


			//添加用户角色
			List<String> roles = JSON.parseArray(jsonObject.get("roles").toString(),String.class);
			for(String role:roles){
				jsonObject.put("role_id",role);
				accountsMapper.insertRole(jsonObject);
			}
			//添加用户部门
			List<String> dids = JSON.parseArray(jsonObject.get("departments").toString(),String.class);
			for(String did:dids){
				jsonObject.put(Account.did.toString(),did);
				accountsMapper.insertDepartment(jsonObject);
			}
			return ResultFactory.success().display();
		}else{
			return ResultFactory.failure("0","手机号已存在",null).display();
		}
	}

	@Override
	public Map<String, Object> accountDetail(JSONObject jsonObject) throws Exception {
		JSONObject account=accountsMapper.findAccountByAID(jsonObject);
		account.put("roles",accountsMapper.getRolesByAID(jsonObject));
		account.put("depart",accountsMapper.getDepartByAID(jsonObject));
		return account;
	}

	@Override
	public Map<String, Object> editPassword(JSONObject jsonObject) throws Exception {
		String oldPassword = Encrypt.encrypt(jsonObject.get(Account.account.toString()).toString()+
				jsonObject.get("oldPassword").toString());
		jsonObject.put("oldPassword",oldPassword);
		Integer isPasswordCorrect=accountsMapper.isPasswordCorrect(jsonObject);
		if(isPasswordCorrect>0){
			String password = Encrypt.encrypt(jsonObject.get(Account.account.toString()).toString()+
				jsonObject.get(Account.password.toString()).toString());
			jsonObject.put(Account.password.toString(),password);
			accountsMapper.updatePasswordByAID(jsonObject);
			return ResultFactory.success().display();
		}else{
			return ResultFactory.failure("0","原密码错误",null).display();
		}
	}

	@Override
	public void updateAccountStatus(JSONObject jsonObject) throws Exception {
			accountsMapper.updateAccountStatus(jsonObject);
	}
}
